<?php
class Admin extends CI_Controller
{
	var $action;
	var $data = array();
	
	
	function __construct()
	{
		parent::__construct();
				
		$this->action = $this->uri->segment(3);
	}
	function index()
	{	
		$this->load->view("admin/dashboard");
	}
	
	function site()
	{
		$this->load->model("managers/site_manager", "site");
		$this->data['site'] = $this->db->get('site')->row();
		
		switch ($this->action) 
		{
		    case "update":
		        $update = $this->site->updateItem(1);
		       redirect("admin");	
		    break;
		    case "edit":
		        $this->load->view("admin/site/site_config", $this->data);
		      	
		    break;
		}
		
	}
	function article()
	{
		$this->load->model("managers/article_manager", "article");
		$category_id = $this->input->post("category_id");
        $this->data['category_id'] = $category_id;
		
		$this->data['articles'] = $this->article->readItems($category_id);
		
		switch ($this->action)
		{
			case "create":
			if($this->article->createItem()) 
			{
				redirect("admin/article/read/");
			} else {
				$this->load->view("admin/article/article_create");
			}
			break;
			case "update":
				$article_id = $this->uri->segment('4');
				$old_alias = $this->article->getAlias($article_id);
				$this->article->updateItem($article_id, $old_alias);
				redirect("admin/article/read/");
			break;
			case "read":
                $this->data['categories'] = $this->db->get("categories")->result();
				$this->load->view("admin/article/articles_list", $this->data);
			break;
			case "edit":
				$this->data['categories'] = $this->db->get("categories")->result();
				$this->data['selected_article'] = $this->article->readItem($this->uri->segment(4));
				$this->load->view("admin/article/article_edit", $this->data);
			break;
			case "make_frontpage":
				$this->article->makeFrontPage($this->input->get("old_id"), $this->input->get("article_id"));
				redirect("admin/article/read");
			break;
		}
	}
	function menu()
	{
		$this->load->model("managers/menu_manager", "menu");
		$this->data['menus'] = $this->menu->readItems();
		switch ($this->action)
		{
			case "read":
				$this->load->view("admin/menu/menus_list", $this->data);
			break;
			case "edit":
				echo "NOT IMPLEMENTED YET";
			break;
			case "create":
				if($this->menu->createItem()) 
		        {
	    			redirect("admin/menu/read/");
				} else {
					$this->load->view("admin/menu/menu_create");
				}
			break;
		}
	}
    function menu_items()
    {
   	    $this->load->model("managers/menu_item_manager", "menu_item");
   	    $this->load->model("managers/menu_manager", "menu");
   	    $this->load->model("managers/article_manager", "article");
   	    $this->load->model("managers/category_manager", "category");
	
		$menu_id = $this->uri->segment(4);
		// SELECT MAIN NAVIGATION FOR NOW
		
		$this->data['menu_items_list'] = $this->menu_item->readMenuItemsList($menu_id);     
		$this->data['articles'] = $this->article->readItems();
		$this->data['categories'] = $this->category->readItems();
	
		switch ($this->action)
		{
			case "update":
				$menu_item_id = $this->uri->segment(4);
				$alias = $this->input->post("alias");
				$this->menu_item->updateItem($menu_item_id, $alias);
				redirect("admin/menu_items/menu_id/1");
			break;
			case "menu_id":
				$this->data['menu_name'] = $this->menu->readItem($menu_id)->title;
				$this->load->view("admin/menu_item/menu_items_list", $this->data);
			break;
			case "edit":
				$menu_item_id = $this->uri->segment(4);
				$this->data['menu_type'] = $this->menu_item->getMenuType($menu_item_id);
				$this->data['menu_item'] =  $this->menu_item->readItem($menu_item_id);
				$this->data['linked_item'] =  $this->menu_item->getLinkedItem($menu_item_id);
				
				$this->load->view("admin/menu_item/menu_item_edit", $this->data);
			break;
			case "select_type":
				
			break;
		}
    }
    function category()
    {
	
		$this->load->model("managers/category_manager", "category");
		$this->data['categories'] = $this->category->readItems();
        
    	switch ($this->action)
		{
			case "create":
        	if($this->category->createItem()) 
	        {
    			redirect("admin/category/read/");
			} else {
				$this->load->view("admin/category/category_create", $this->data);
			}
					
			break;
			case "update":
				$category_id = $this->uri->segment(4);
                $this->category->updateItem($category_id);
				redirect("admin/category/read/");
               
			break;
			case "read":
				$this->load->view("admin/category/categories_list", $this->data);
			break;
			case "edit":
                $category_id = $this->uri->segment(4);
                $this->data['category'] = $this->category->readItem($category_id);
				$this->load->view("admin/category/category_edit", $this->data);
			break;
			case "make_frontpage":
				
			break;
		}
	}
	function install()
	{
		if(read_file('install'))
		{
			
		}
	}
	function ckeditor()
	{
		$this->load->helper('ckeditor');
		
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
		
			//ID of the textarea that will be replaced
			'id' 	=> 	'content',
			'path'	=>	'js/ckeditor',
		
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
					
			),
		
			//Replacing styles from the "Styles tool"
			'styles' => array(
			
				//Creating a new style named "style 1"
				'style 1' => array (
					'name' 		=> 	'Blue Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 			=> 	'Blue',
						'font-weight' 		=> 	'bold'
					)
				),
				
				//Creating a new style named "style 2"
				'style 2' => array (
					'name' 		=> 	'Red Title',
					'element' 	=> 	'h2',
					'styles' => array(
						'color' 			=> 	'Red',
						'font-weight' 		=> 	'bold',
						'text-decoration'	=> 	'underline'
					)
				)				
			)
		);
		
		$this->data['ckeditor_2'] = array(
		
			//ID of the textarea that will be replaced
			'id' 	=> 	'content_2',
			'path'	=>	'js/ckeditor',
		
			//Optionnal values
			'config' => array(
				'width' 	=> 	"550px",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
				'toolbar' 	=> 	array(		//Setting a custom toolbar
					array('Bold', 'Italic'),
					array('Underline', 'Strike', 'FontSize'),
					array('Smiley'),
					'/'
				)
			),
		
			//Replacing styles from the "Styles tool"
			'styles' => array(
			
				//Creating a new style named "style 1"
				'style 3' => array (
					'name' 		=> 	'Green Title',
					'element' 	=> 	'h3',
					'styles' => array(
						'color' 			=> 	'Green',
						'font-weight' 		=> 	'bold'
					)
				)
								
			)
		);
		
		switch ($this->action)
			{
				case "save":
					print_r($this->input->post("text1"));
				break;
			}
		
		$this->load->view('ckeditor', $this->data);
	}
}